package net.kazed.nextaction.toplevel.activity;

import net.kazed.nextaction.R;
import net.kazed.nextaction.context.activity.ContextTreeActivity;
import net.kazed.nextaction.context.activity.ContextsActivity;
import net.kazed.nextaction.context.activity.DueTasksActivity;
import net.kazed.nextaction.context.activity.InboxActivity;
import net.kazed.nextaction.context.activity.StartedTasksActivity;
import net.kazed.nextaction.database.Task;
import net.kazed.nextaction.tools.ToolsActivity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * Displays a list of the main activities.
 */
public class TopLevelActivity extends ListActivity {
    private static final String cTag = "TopLevelActivity";

    private static final int INBOX = 0;
    private static final int CONTEXTS = INBOX + 1;
    private static final int STARTED_TASKS = CONTEXTS + 1;
    private static final int DUE_TASKS = STARTED_TASKS + 1;
    private static final int TOOLS = DUE_TASKS + 1;
//    private static final int PREFERENCES = 3;
    private static final int HELP = TOOLS + 1;
    
	@Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setDefaultKeyMode(DEFAULT_KEYS_SHORTCUT);
    }
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
//        MenuUtils.addPrefsHelpMenuItems(menu);
        return true;
    }

	@Override
	protected void onResume() {
        Log.d(cTag, "onResume+");
		super.onResume();

		// get counts for each item
		Cursor cursor;
		String[] projection = new String[] {"_id"};
		cursor = getContentResolver().query(Task.CONTENT_URI, projection, Task.INBOX_SELECTION, null, null);
		int inboxCount = cursor.getCount();
		cursor.close();
		
        String[] perspectives = new String[] {
                        getResources().getString(R.string.toplevel_inbox) + " (" + inboxCount + ")",
                        getResources().getString(R.string.toplevel_context),
                        getResources().getString(R.string.toplevel_started_tasks),
                        getResources().getString(R.string.toplevel_due_tasks),
                        getResources().getString(R.string.toplevel_tools),
                        getResources().getString(R.string.toplevel_help)
        };
        
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, R.layout.list_item_view,
              R.id.name, perspectives);
        setListAdapter(adapter);
	}

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
//        if (MenuUtils.checkCommonItemsSelected(item, this, -1)) {
//        	return true;
//        }
        return super.onOptionsItemSelected(item);
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
//		MenuUtils.checkCommonItemsSelected(position + MenuUtils.INBOX_ID, this, -1, false);
      switch (position) {
      case INBOX:
         startActivity(new Intent(this, InboxActivity.class));
         break;
      case CONTEXTS:
         startActivity(new Intent(this, ContextsActivity.class));
         break;
      case STARTED_TASKS:
          startActivity(new Intent(this, StartedTasksActivity.class));
          break;
      case DUE_TASKS:
          startActivity(new Intent(this, DueTasksActivity.class));
          break;
      case HELP:
         startActivity(new Intent(this, HelpActivity.class));
         break;
//      case PREFERENCES:
//         startActivity(new Intent(this, GlobalPreferenceActivity.class));
//         break;
      case TOOLS:
         startActivity(new Intent(this, ToolsActivity.class));
         break;
      default:
         startActivity(new Intent(this, ContextTreeActivity.class));
         break;
       }       
    }

}
